package com.ruoyi.web.controller.common;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFileOne;
import com.ruoyi.system.service.ISysFileOneService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 上传文件Controller
 * 
 * @author ruoyi
 * @date 2022-09-12
 */
@RestController
@RequestMapping("/system/one")
@Slf4j
public class SysFileOneController extends BaseController
{
    @Autowired
    private ISysFileOneService sysFileOneService;

    /**
     * 查询上传文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:one:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFileOne sysFileOne)
    {
        startPage();
        List<SysFileOne> list = sysFileOneService.selectSysFileOneList(sysFileOne);
        return getDataTable(list);
    }

    /**
     * 导出上传文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:one:export')")
    @Log(title = "上传文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFileOne sysFileOne)
    {
        List<SysFileOne> list = sysFileOneService.selectSysFileOneList(sysFileOne);
        ExcelUtil<SysFileOne> util = new ExcelUtil<SysFileOne>(SysFileOne.class);
        util.exportExcel(response, list, "上传文件数据");
    }

    /**
     * 获取上传文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:one:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return AjaxResult.success(sysFileOneService.selectSysFileOneByFileId(fileId));
    }

    /**
     * 新增上传文件
     */
    @PreAuthorize("@ss.hasPermi('system:one:add')")
    @Log(title = "上传文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFileOne sysFileOne)
    {
        if(sysFileOne.getFileUrl()!=null){
            sysFileOne.setFileUrl(sysFileOne.getFileUrl().replace("/blood","https://xzzgxrmyy.com/blood"));
           // logger.info(sysFileOne.getFileUrl());
            sysFileOne.setRemark(getFileSrc(sysFileOne.getFileUrl()));
        };
        return toAjax(sysFileOneService.insertSysFileOne(sysFileOne));
    }

    /**
     * 修改上传文件
     */
    @PreAuthorize("@ss.hasPermi('system:one:edit')")
    @Log(title = "上传文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFileOne sysFileOne)
    {
        if(sysFileOne.getFileUrl()!=null){
            sysFileOne.setFileUrl(sysFileOne.getFileUrl().replace("/blood","https://xzzgxrmyy.com/blood"));
            //logger.info(sysFileOne.getFileUrl());
            sysFileOne.setRemark(getFileSrc(sysFileOne.getFileUrl()));
        };

        return toAjax(sysFileOneService.updateSysFileOne(sysFileOne));
    }

    /**
     * 删除上传文件
     */
    @PreAuthorize("@ss.hasPermi('system:one:remove')")
    @Log(title = "上传文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(sysFileOneService.deleteSysFileOneByFileIds(fileIds));
    }


    public  String getFileSrc(String html){
        Document doc = Jsoup.parse(html);//解析html

        Elements imgList = doc.body().getElementsByTag("img");//获取所有img标签
        for(Element element : imgList){
            return element.attr("src");//获取src属性值
        }
        return null;
    }
}
